﻿using Lab2.vorkos_dev_doubleToMoney.Localization;
using System;
using System.Globalization;

namespace Lab2.vorkos_dev_doubleToMoney
{
    public class Program
    {
        public static void Main()
        {
            double   MoneyQuantity = 0;
            int     coinsQuantity = 0;
            int     quantity = 0;
            string fullStringResult = String.Empty;
            string[] args = Environment.CommandLine.Split(' ');
            int hundred = 100;

            if (args[1] == Constantsru.TEST_ARG1 || args[1] == Constantsru.TEST_ARG1) 
            {
                TestingCollection.testApplication();
            }
            else
            {
                MoneyQuantity = Enter();
                coinsQuantity = (int)((MoneyQuantity * hundred) % hundred);
                quantity = (int)Math.Floor(MoneyQuantity);
                fullStringResult = String.Concat(MoneyToString(quantity).Remove(0, 1), CoinsToString(coinsQuantity));
                Console.WriteLine(fullStringResult);
            }
            Console.ReadLine();
        }

        public static double Enter()
        {
            double MoneyQuantity = 0;
            const int thousand = 1000;

            Console.Write(Constantsru.ENTER_VALUE);
            while (!double.TryParse(Console.ReadLine(), out MoneyQuantity))
            {
                Console.WriteLine(Constantsru.ERROR_MESSAGE);
            }
            if (MoneyQuantity >= thousand * thousand * thousand)
            {
                Console.WriteLine(Constantsru.MONEY_OVERFLOW);
                Console.ReadKey();
                return 0;
            }
            else if (MoneyQuantity<0)
            {
                Console.WriteLine(Constantsru.MONEY_LOW);
                Console.ReadKey();
                return 0;
            }
            return MoneyQuantity;
        }

        public static string MoneyToString(int quantity)
        {
            string Result = String.Empty;
            int million = 1000000;
            const int thousand = 1000;
            int     hundred =   100;
            int     ten =       10;
            int[] Digits = new int[6] {0,0,0,0,0,0};

            Digits[5] = (quantity % (hundred * thousand * thousand)) / (ten * thousand * thousand);
            Digits[4] = (quantity % (ten * thousand * thousand)) / (1 * thousand * thousand);
            Digits[3] = (quantity % (hundred * thousand)) / (ten * thousand);
            Digits[2] = (quantity % (ten * thousand)) / thousand;
            Digits[1] = (quantity % hundred) / ten;
            Digits[0] = quantity % ten;
            if (quantity > (thousand * thousand-1)) 
            if (Digits[4] == 0 || Digits[5] == 1)
            {
                Result = String.Concat(Result,
                    ThreeDigitsToString((quantity % (thousand * thousand * thousand)) / million));
                Result = String.Concat(Result, Constantsru.MILLIONS);
            }
            else if (Digits[4] == 1)
            {
                Result = String.Concat(Result,
                    ThreeDigitsToString((quantity % (thousand * thousand * thousand)) / million));
                Result = String.Concat(Result, Constantsru.MILLION);
            }
            else if (Digits[4] < 5)
            {
                Result = String.Concat(Result,
                    ThreeDigitsToString((quantity % (thousand * thousand * thousand)) / million));
                Result = String.Concat(Result, Constantsru.MILLION1);
            }
            else if (quantity > (thousand * thousand-1))
            {
                Result = String.Concat(Result,
                    ThreeDigitsToString((quantity % (thousand * thousand * thousand)) / million));
                Result = String.Concat(Result, Constantsru.MILLIONS1);
            }

            if ((quantity > (thousand-1))
                && ((quantity % (thousand * thousand)) / thousand)>1 )
            if (Digits[2] == 0 || Digits[3] == 1)
            {
                Result = String.Concat(Result,
                    ThreeDigitsToString((quantity % (thousand * thousand)) / thousand));
                Result = String.Concat(Result, Constantsru.THOUSANDS);
            }
            else if (Digits[2] == 1)
            {
                Result = String.Concat(Result,
                    ThreeDigitsToString(((quantity % (thousand * thousand)) / thousand) - (quantity % (thousand * ten)) / thousand));
                Result = String.Concat(Result, Constantsru.ONETHOUSAND);
            }
            else if (Digits[2] == 2)
            {
                Result = String.Concat(Result,
                    ThreeDigitsToString(((quantity % (thousand * thousand)) / thousand) - (quantity % (thousand * ten)) / thousand));
                Result = String.Concat(Result, Constantsru.TWOTHOUSANDS);
            }
            else if (Digits[2] < 5)
            {
                Result = String.Concat(Result,
                    ThreeDigitsToString((quantity % (thousand * thousand)) / thousand));
                Result = String.Concat(Result, Constantsru.THOUSANDS1);
            }
            else if (Digits[2] <= 9)
            {
                Result = String.Concat(Result, ThreeDigitsToString((quantity % (thousand * thousand)) / thousand));
                Result = String.Concat(Result, Constantsru.THOUSANDS2);
            }

            Result = String.Concat(Result, ThreeDigitsToString(quantity));

            if (quantity==0)
            {
                Result = String.Concat(Result, Constantsru.ZERO_MONEY);
            }
            else if (Digits[0] == 0 || Digits[1]==1)
            {
                Result = String.Concat(Result, Constantsru.MONEY1);
            }
            else if (Digits[0] == 1)
            {
                Result = String.Concat(Result, Constantsru.MONEY2);
            }
            else if (Digits[0] < 5)
            { 
                Result = String.Concat(Result, Constantsru.MONEY3); 
            }
            else 
            { 
                Result = String.Concat(Result, Constantsru.MONEY1); 
            }
            return Result;
        }

        public static string CoinsToString(int coinQuantity)
        {
            string Result = String.Empty;
            if (coinQuantity == 0)
            {
                Result = Constantsru.ZERO_COIN;
            }
            else if (coinQuantity % 10 == 0 || (coinQuantity > 9 && coinQuantity < 20))
            {
                Result = ThreeDigitsToString(coinQuantity);
                Result = String.Concat(Result, Constantsru.COINS);
            }
            else if (coinQuantity % 10 == 1)
            {
                Result = ThreeDigitsToString(coinQuantity % 100 - coinQuantity % 10);
                Result = String.Concat(Result, Constantsru.COIN);
            }
            else if (coinQuantity % 10 == 2)
            {
                Result = ThreeDigitsToString(coinQuantity % 100 - coinQuantity % 10);
                Result = String.Concat(Result, Constantsru.TWOCOINS);
            }
            else if (coinQuantity % 10 < 5)
            {
                Result = ThreeDigitsToString((int)coinQuantity);
                Result = String.Concat(Result, Constantsru.COINS1);
            }
            else if (coinQuantity % 10 < 10)
            {
                Result = ThreeDigitsToString((int)coinQuantity);
                Result = String.Concat(Result, Constantsru.COINS);
            }
            else
            {
                Result = ThreeDigitsToString((int)coinQuantity);
                Result = String.Concat(Result, Constantsru.ERROR);
            }
            return Result;
        }

        public static string ThreeDigitsToString(int ThreeDigitQuantity)
        {
            const int thousand = 1000;
            int hundred = 100;
            int ten = 10;

            string Result = String.Empty;
            int[] Digits = new int[3] { 0, 0, 0};
            Digits[2] = (ThreeDigitQuantity % thousand) / hundred;
            Digits[1] = (ThreeDigitQuantity % hundred) / 10;
            Digits[0] = ThreeDigitQuantity % ten;

            switch (Digits[2])
            {
                case 1:
                    Result = String.Concat(Result, Constantsru.ONEHUNDRED);
                    break;
                case 2:
                    Result = String.Concat(Result, Constantsru.TWOHUNDREDS);
                    break;
                case 3:
                    Result = String.Concat(Result, Constantsru.THREEHUNDREDS);
                    break;
                case 4:
                    Result = String.Concat(Result, Constantsru.FOURHUNDREDS);
                    break;
                case 5:
                    Result = String.Concat(Result, Constantsru.FIVEHUNDREDS);
                    break;
                case 6:
                    Result = String.Concat(Result, Constantsru.SIXHUNDREDS);
                    break;
                case 7:
                    Result = String.Concat(Result, Constantsru.SEVENHUNDREDS);
                    break;
                case 8:
                    Result = String.Concat(Result, Constantsru.EIGHTHUNDREDS);
                    break;
                case 9:
                    Result = String.Concat(Result, Constantsru.NINEHUNDREDS);
                    break;
            }
            if (Digits[1] == 1)
            {
                switch (Digits[0])
                {
                    case 0:
                        Result = String.Concat(Result, Constantsru.TEN);
                        break;
                    case 1:
                        Result = String.Concat(Result, Constantsru.ELEVEN);
                        break;
                    case 2:
                        Result = String.Concat(Result, Constantsru.TWELWE);
                        break;
                    case 3:
                        Result = String.Concat(Result, Constantsru.THRETEEN);
                        break;
                    case 4:
                        Result = String.Concat(Result, Constantsru.FOURTEEN);
                        break;
                    case 5:
                        Result = String.Concat(Result, Constantsru.FIVETEEN);
                        break;
                    case 6:
                        Result = String.Concat(Result, Constantsru.SIXTEEN);
                        break;
                    case 7:
                        Result = String.Concat(Result, Constantsru.SEVENTEEN);
                        break;
                    case 8:
                        Result = String.Concat(Result, Constantsru.EIGHTTEEN);
                        break;
                    case 9:
                        Result = String.Concat(Result, Constantsru.NINETEEN);
                        break;
                }
            }
            else
            {

                switch (Digits[1])
                    {

                        case 2:
                            Result = String.Concat(Result, Constantsru.TWENTY);
                            break;
                        case 3:
                            Result = String.Concat(Result, Constantsru.THIRTY);
                            break;
                        case 4:
                            Result = String.Concat(Result, Constantsru.FOURTY);
                            break;
                        case 5:
                            Result = String.Concat(Result, Constantsru.FIFTY);
                            break;
                        case 6:
                            Result = String.Concat(Result, Constantsru.SIXTY);
                            break;
                        case 7:
                            Result = String.Concat(Result, Constantsru.SEVENTY);
                            break;
                        case 8:
                            Result = String.Concat(Result, Constantsru.EIGHTY);
                            break;
                        case 9:
                            Result = String.Concat(Result, Constantsru.NINETY);
                            break;
                    }
                 switch (Digits[0])
                    {
                    case 0:
                        Result = String.Concat(Result, String.Empty);
                        break;
                    case 1:
                        Result = String.Concat(Result, Constantsru.ONE);
                        break;
                    case 2:
                        Result = String.Concat(Result, Constantsru.TWO);
                        break;
                    case 3:
                        Result = String.Concat(Result, Constantsru.THREE);
                        break;
                    case 4:
                        Result = String.Concat(Result, Constantsru.FOUR);
                        break;
                    case 5:
                        Result = String.Concat(Result, Constantsru.FIVE);
                        break;
                    case 6:
                        Result = String.Concat(Result, Constantsru.SIX);
                        break;
                    case 7:
                        Result = String.Concat(Result, Constantsru.SEVEN);
                        break;
                    case 8:
                        Result = String.Concat(Result, Constantsru.EIGHT);
                        break;
                    case 9:
                        Result = String.Concat(Result, Constantsru.NINE);
                        break;
                    }
            }
            return Result;
        }
    }
}
